<template>
	<view>
		<u-mask :duration="250" :mask-click-able="false" :custom-style="maskStyle" :show="show">
			<view class="board">
				<image class="bg-img" src="https://mpwx.1-1dr.com/static/home/borad/board_background.png"
					mode="aspectFill" />
				<image class="notice-img" src="https://mpwx.1-1dr.com/static/home/borad/board_notice.png"
					mode="aspectFill" />
				<swiper class="swiper" :current="currentIndex" @change="change" :duration="500">
					<swiper-item class="u-swiper-item" v-for="(item, index) in activeList" :key="index">
						<view v-if="item.title" class="board-title">
							{{ item.title }}
						</view>
						<scroll-view scroll-y="true" style="height:100%">
							<view v-if="item.content" class="board-content">
								{{ item.content }}
							</view>
						</scroll-view>
					</swiper-item>
				</swiper>
				<!-- 指示器 -->
				<view class="swiper-indicator">
					<block>
						<view class="indicator-item-round"
							:class="{ 'indicator-item-round-active': index == currentIndex }"
							v-for="(item, index) in activeList" :key="index"></view>
					</block>
				</view>
				<!-- 底部按钮 -->
				<view class="btnBox">
					<view class="btn" @click="read">
						我知道了
					</view>
				</view>
			</view>
		</u-mask>
	</view>
</template>

<script>
	export default {
		name: "board",
		props: {
			list: {
				type: Array,
				default: () => [],
			}
		},
		data() {
			return {
				show: false,
				currentId: null,
				currentIndex: 0,
				tempArr: [] //临时已读数组
			};
		},

		mounted() {
			if (this.activeList.length > 0) {
				this.show = true;
				this.currentId = this.activeList[0].id
			}
		},

		computed: {
			//遮罩样式
			maskStyle() {
				return {
					display: 'flex',
					justifyContent: 'center',
					alignItems: 'center',
				}
			},
			//已读数组
			readArr() {
				return uni.getStorageSync("board-readArr") || []
			},
			//动态公告
			activeList() {
				return this.list.filter(item => !this.readArr.includes(item.id))
			},
		},
		watch: {
			currentId(val) {
				!this.tempArr.includes(val) && this.tempArr.push(val)
				console.log('---临时已读', this.tempArr)
			}
		},
		methods: {
			change(e) {
				this.currentIndex = e.detail.current;
				this.currentId = this.activeList[this.currentIndex].id
			},
			read() {
				let arr = this.readArr.concat(this.tempArr)
				uni.setStorageSync("board-readArr", arr)
				this.show = false
				console.log('---设置本地已读', arr)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.board {
		height: 742rpx;
		width: 600rpx;
		position: relative;
		background: $color-white;
		border-radius: 16rpx;
		padding-top: 174rpx;
		padding-bottom: 24px;

		.notice-img {
			position: absolute;
			top: -110rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 338rpx;
			height: 284rpx;
		}

		.bg-img {
			position: absolute;
			top: 0;
			width: 600rpx;
			height: 294rpx;
		}

		.swiper {
			height: 400rpx;

			.board-title {
				font-size: 38rpx;
				text-align: center;
				color: $color-primary;
			}

			.board-content {
				padding: 32rpx 48rpx 55rpx;
			}
		}

		.swiper-indicator {
			margin-top: 10rpx;
			padding: 0 24rpx;
			position: absolute;
			display: flex;
			justify-content: center;
			width: 100%;
			z-index: 1;

			.indicator-item-round {
				width: 14rpx;
				height: 14rpx;
				margin: 0 6rpx;
				border-radius: 20rpx;
				transition: all 0.5s;
				background-color: rgba(191, 55, 90, 0.3);
			}

			.indicator-item-round-active {
				width: 34rpx;
				background-color: $color-primary;
			}
		}

		.btnBox {
			position: absolute;
			bottom: 24rpx;
			width: 100%;
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.btn {
				flex: 1;
				margin: 0 32rpx;
				line-height: 84rpx;
				font-size: $font-size-big;
				text-align: center;
				color: $color-white;
				background: $color-primary;
				border-radius: 55rpx;
			}
		}
	}
</style>
